//
//  AddTaskViewController.swift
//  willdo
//
//  Created by zz on 3/27/18.
//  Copyright © 2018 will do. All rights reserved.
//

import UIKit
import McPicker
import PGDatePicker

class AddTaskViewController: UIViewController, PGDatePickerDelegate {
    
    var selectMember:InternalMemberModel?
    
    let topView = UIView()
    let priorityLabel = UILabel()
    let prioritySelect = UIButton()
    let deadTimeLabel = UILabel()
    let deadTimeSelect = UIButton()

    
    let memberView = UIView()
    let memberLabel = UILabel()
    let memberName = UILabel()
    let memberBtn = UIButton()
    
    let taskView = UIView()
    let taskMsgLabel = UILabel()
    let taskImg = UIButton()
    let taskMsgEdit = UITextView()
    
    let copyView = UIView()
    let copyMember = UILabel()
    let copyBtn = UIButton()
    
    let submitBtn = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.edgesForExtendedLayout = UIRectEdge(rawValue: 0)
        self.title = "添加任务"
        setupView()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if selectMember != nil{
            memberName.text = selectMember?.name
            memberName.isHidden = false
        }
    }

    @objc func addMember(){
        let selectVC = InternalMemberViewController()
        selectVC.modle = CONTACT_INTERNAL_MODLE
        selectVC.presentController = self
        navigationController?.pushViewController(selectVC, animated: true)
    }
    
    
    // MARK: 界面部分
    func setupView(){
        view.backgroundColor = BACKGROUND_GRAY
        view.addSubview(topView)
        view.addSubview(taskView)
        view.addSubview(memberView)
        view.addSubview(copyView)
        
        topView.backgroundColor = UIColor.white
        setupTopView()
        topView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(18)
            make.width.equalToSuperview().offset(-20)
            make.centerX.equalToSuperview()
            make.height.equalTo(146)
        }
        
        memberView.backgroundColor = UIColor.white
        memberView.snp.makeConstraints { (make) in
            make.top.equalTo(topView.snp.bottom).offset(18)
            make.left.right.equalTo(topView)
            make.height.equalTo(36)
        }
        
        taskView.backgroundColor = UIColor.white
        taskView.snp.makeConstraints { (make) in
            make.top.equalTo(memberView.snp.bottom).offset(18)
            make.left.right.equalTo(memberView)
            make.height.equalTo(160)
        }
        
        copyView.backgroundColor = UIColor.white
        copyView.snp.makeConstraints { (make) in
            make.top.equalTo(taskView.snp.bottom).offset(18)
            make.left.right.equalTo(taskView)
            make.height.equalTo(memberView)
        }
        setupUseless()
        
        submitBtn.backgroundColor = UIColor.white
        view.addSubview(submitBtn)
        let attributeTitle = NSAttributedString(string: "发布任务", attributes:
            [NSAttributedStringKey.font : UIFont(name: "PingFang SC", size: 17)!,
             NSAttributedStringKey.foregroundColor:UIColor(hexString:"#d14d4e")])
        submitBtn.setAttributedTitle(attributeTitle, for: .normal)
        submitBtn.snp.makeConstraints { (make) in
            make.width.equalTo(topView)
            make.top.equalTo(copyView.snp.bottom).offset(17)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
        }
        
    }
    
    func setupTopView(){
        let redImg = UIImageView(image: UIImage(named:"sign_picture"))
        let taskName = UITextField()
        let leaderLabel = UILabel()
        let leaderName = UILabel()
        
        topView.addSubview(redImg)
        topView.addSubview(taskName)
        topView.addSubview(leaderLabel)
        topView.addSubview(leaderName)
        topView.addSubview(priorityLabel)
        topView.addSubview(prioritySelect)
        topView.addSubview(deadTimeLabel)
        topView.addSubview(deadTimeSelect)
        redImg.snp.makeConstraints { (make) in
            make.width.height.equalTo(13)
            make.left.equalToSuperview().offset(13)
            make.centerY.equalToSuperview().multipliedBy(0.25)
        }
        
        taskName.placeholder = "请输入任务名称"
        taskName.font = UIFont.init(name: "PingFang SC", size: 17)
        taskName.textColor = UIColor(hexString: "#040000")
        taskName.snp.makeConstraints { (make) in
            make.left.equalTo(redImg.snp.right).offset(12)
            make.right.equalToSuperview()
            make.centerY.equalTo(redImg)
            make.height.equalTo(24)
        }
        let sepLine0 = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
        topView.addSubview(sepLine0)
        sepLine0.backgroundColor = BACKGROUND_GRAY
        sepLine0.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview().multipliedBy(0.5)
            make.left.equalTo(redImg.snp.centerX)
            make.right.equalToSuperview()
            make.height.equalTo(1)
        }
        let leaderImg = UIImageView(image: UIImage(named:"portrait_picture"))
        topView.addSubview(leaderImg)
        leaderImg.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview().multipliedBy(0.75)
            make.left.equalToSuperview().offset(24)
            make.width.height.equalTo(13)
        }
        leaderLabel.text = "指派人:"
        leaderLabel.font = UIFont(name: "PingFang SC", size: 15)
        leaderLabel.textColor = UIColor(hexString:"#040000")
        leaderLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(leaderImg)
            make.left.equalTo(leaderImg.snp.right).offset(13)
            make.height.equalTo(leaderImg)
        }
//        leaderImg.backgroundColor = UIColor.orange
//        leaderImg.snp.makeConstraints { (make) in
//            make.centerY.equalTo(leaderLabel)
//            make.height.width.equalTo(16)
//            make.left.equalTo(leaderLabel.snp.right).offset(1)
//        }
        leaderName.text = "张三"
        leaderName.font = UIFont(name:"PingFang SC", size: 14)
        leaderName.textColor = UIColor.black
        leaderName.snp.makeConstraints { (make) in
            make.centerY.equalTo(leaderLabel)
            make.left.equalTo(leaderLabel.snp.right).offset(35)
            make.height.equalTo(leaderLabel)
        }
        
        let sepLine1 = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
        topView.addSubview(sepLine1)
        sepLine1.backgroundColor = BACKGROUND_GRAY
        sepLine1.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.right.height.equalTo(sepLine0)
        }
        let priorityImg = UIImageView(image: UIImage(named:"emergy_picture"))
        topView.addSubview(priorityImg)
        priorityImg.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview().multipliedBy(1.25)
            make.left.width.height.equalTo(leaderImg)
        }
        priorityLabel.text = "优先程度:"

        priorityLabel.textColor = leaderLabel.textColor
        priorityLabel.font = leaderLabel.font
        priorityLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(priorityImg)
            make.left.height.equalTo(leaderLabel)
        }
        let priorityTitle = NSAttributedString(string: "请选择                                                    ", attributes:
            [NSAttributedStringKey.font : UIFont(name: "PingFang SC", size: 14)!,
             NSAttributedStringKey.foregroundColor:UIColor(hexString:"#c1c1c1")])
        prioritySelect.setAttributedTitle(priorityTitle, for: .normal)
        prioritySelect.addTarget(self, action: #selector(changePriority), for: .touchUpInside)
        prioritySelect.snp.makeConstraints { (make) in
            make.centerY.equalTo(priorityLabel)
            make.left.equalTo(leaderName)
            make.height.equalTo(leaderName).offset(15)
        }
        
        let sepLine2 = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
        topView.addSubview(sepLine2)
        sepLine2.backgroundColor = BACKGROUND_GRAY
        sepLine2.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview().multipliedBy(1.5)
            make.left.right.height.equalTo(sepLine1)
        }
        
        let deadImg = UIImageView(image:UIImage(named:"date_picture"))
        topView.addSubview(deadImg)
        deadImg.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview().multipliedBy(1.75)
            make.left.width.height.equalTo(priorityImg)
        }
        deadTimeLabel.text = "截止日期:"
        deadTimeLabel.textColor = priorityLabel.textColor
        deadTimeLabel.font = priorityLabel.font
        deadTimeLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(deadImg)
            make.left.height.equalTo(priorityLabel)
        }
        let deadTimeTitle = NSAttributedString(string: "请选择                          ", attributes:
            [NSAttributedStringKey.font : UIFont(name: "PingFang SC", size: 14)!,
             NSAttributedStringKey.foregroundColor:UIColor(hexString:"#c1c1c1")])
        deadTimeSelect.setAttributedTitle(deadTimeTitle, for: .normal)
        deadTimeSelect.setTitleColor(leaderName.textColor, for: .normal)
        deadTimeSelect.addTarget(self, action: #selector(changeDeadTime), for: .touchUpInside)
        deadTimeSelect.snp.makeConstraints { (make) in
            make.centerY.equalTo(deadTimeLabel)
            make.left.height.equalTo(prioritySelect)
        }
        
        let Line = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
        topView.addSubview(Line)
        Line.backgroundColor = BACKGROUND_GRAY
        Line.snp.makeConstraints { (make) in
            make.top.equalTo(sepLine0)
            make.bottom.equalToSuperview()
            make.width.equalTo(1)
            make.left.equalTo(leaderName).offset(-10)
        }
    }
    
    @objc func changePriority(){
//        McPicker.showAsPopover(data: [["请选择","紧急","重要","普通","次要","不急"]], fromViewController: self, sourceView: self.prioritySelect) { (selections: [Int:String]) in
//            self.prioritySelect.setTitle(selections[0], for: .normal)
//        }
        McPicker.show(data: [["请选择                                                    ",
                              "紧急                                                    ",
                              "重要                                                    ",
                              "普通                                                    ",
                              "次要                                                    ",
                              "不急                                                    "]])
        { (selections: [Int:String]) in
            let title = NSAttributedString(string: selections[0]!, attributes:
                [NSAttributedStringKey.font : UIFont(name: "PingFang SC", size: 14)!,
                 NSAttributedStringKey.foregroundColor:UIColor(hexString:"#040000")])
            self.prioritySelect.setAttributedTitle(title, for: .normal)
        }
    
    }
    
    @objc func changeDeadTime(){
        let datePicker = PGDatePickManager().datePicker
//        datePicker.show()
        datePicker?.delegate = self
        datePicker?.datePickerMode = .date
    }
    
    func datePicker(_ datePicker: PGDatePicker!, didSelectDate dateComponents: DateComponents!) {
        let month = dateComponents.month?.description
        let day = dateComponents.day?.description
        let selectTime = "\(month!)月\(day!)日                                          "
        let title = NSAttributedString(string: selectTime, attributes:
            [NSAttributedStringKey.font : UIFont(name: "PingFang SC", size: 14)!,
             NSAttributedStringKey.foregroundColor:UIColor(hexString:"#040000")])
        self.deadTimeSelect.setAttributedTitle(title, for: .normal)
    }
    
    func setupUseless(){
        let redImg0 = UIImageView(image: UIImage(named:"sign_picture"))
        memberView.addSubview(redImg0)
        redImg0.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(13)
            make.width.height.equalTo(13)
        }
        memberView.addSubview(memberLabel)
        memberView.addSubview(memberBtn)
        memberLabel.text = "负责人:"
        memberLabel.font = UIFont(name: "PingFang SC", size: 15)
        memberLabel.textColor = UIColor(hexString:"#040000")
        memberLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(redImg0)
            make.left.equalTo(redImg0.snp.right).offset(12)
            make.height.equalTo(23)
        }
        memberView.addSubview(memberName)
        memberName.isHidden = true
        memberName.font = UIFont(name: "PingFangSC", size: 15)
        memberName.textColor = UIColor(hexString:"#040000")
        memberName.snp.makeConstraints { (make) in
            make.centerY.centerY.equalTo(memberLabel)
            make.left.equalTo(memberLabel.snp.right).offset(12)
            make.height.equalTo(23)
        }
        
        memberBtn.setImage(UIImage(named:"plus_button")?.withRenderingMode(.alwaysOriginal), for: .normal)
        memberBtn.addTarget(self, action: #selector(addMember), for: .touchUpInside)
        memberBtn.setTitleColor(UIColor.blue, for: .normal)
        memberBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-12)
            make.height.width.equalTo(18)
        }
        
        let redImg1 = UIImageView(image: UIImage(named:"sign_picture"))
        taskView.addSubview(redImg1)
        redImg1.snp.makeConstraints { (make) in
            make.top.left.equalToSuperview().offset(13)
            make.width.height.equalTo(13)
        }
        taskView.addSubview(taskMsgLabel)
        taskView.addSubview(taskImg)
        taskView.addSubview(taskMsgEdit)
        taskMsgLabel.text = "任务说明:"
        taskMsgLabel.font = UIFont(name: "PingFang SC", size: 15)
        taskMsgLabel.textColor = UIColor(hexString:"#040000")
        taskMsgLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(redImg1)
            make.left.height.equalTo(memberLabel)
        }
        
        taskImg.setImage(UIImage(named:"picture_button")?.withRenderingMode(.alwaysOriginal), for: .normal)
        taskImg.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-12)
            make.width.equalTo(21)
            make.height.equalTo(18)
            make.centerY.equalTo(taskMsgLabel)
        }
        taskMsgEdit.layer.borderWidth = 1
        taskMsgEdit.layer.cornerRadius = 5
        taskMsgEdit.layer.borderColor = UIColor(hexString: "#ebebeb").cgColor
        taskMsgEdit.contentInset = UIEdgeInsetsMake(3, 4, 3, 4)
        taskMsgEdit.font = UIFont(name: "PingFang SC", size: 12)
        taskMsgEdit.snp.makeConstraints { (make) in
            make.top.equalTo(taskMsgLabel.snp.bottom).offset(11)
            make.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-12)
            make.bottom.equalToSuperview().offset(-11)
        }
        
        let redImg2 = UIImageView(image: UIImage(named:"sign_picture"))
        copyView.addSubview(redImg2)
        redImg2.snp.makeConstraints { (make) in
            make.left.equalTo(redImg1)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(13)
        }
        copyView.addSubview(copyMember)
        copyView.addSubview(copyBtn)
        copyMember.text = "抄送人"
        copyMember.font = UIFont(name: "PingFang SC", size: 15)
        copyMember.textColor = UIColor(hexString:"#040000")
        copyMember.snp.makeConstraints { (make) in
            make.centerY.equalTo(redImg2)
            make.left.equalTo(memberLabel)
            make.height.equalTo(memberLabel)
        }
        copyBtn.setImage(UIImage(named:"plus_button")?.withRenderingMode(.alwaysOriginal), for: .normal)
        copyBtn.setTitleColor(UIColor.black, for: .normal)
        copyBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-12)
            make.height.width.equalTo(18)
        }
    }
    
}






